home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HPAVC
/
HPAVC CD-ROM.iso
/
ARASAN_S.ZIP
/
ARRAY.H
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-16
|
4KB
|
163 lines
// Copyright 1993 by Jon Dart. All Rights Reserved.
#ifndef _ARRAY_H
#define _ARRAY_H
#include <stddef.h>
#include <assert.h>
#include "types.h"
typedef unsigned Index;
template <class Contents>
class Array
{
public:
// defines a simple, type-safe, expandable array class.
Array( const Index initial_size = 100, const Boolean initialize = True );
virtual ~Array();
Array( const Array<Contents> & );
// copy constructor
Array<Contents> & operator = (const Array<Contents> & );
// assignment operator
Contents &operator [] (const Index) const;
// Note: we return a reference so that array[k] = stuff works.
const Contents *get_data() const
{
return data;
}
int size() const
{
// return the last valid index + 1
return data_size;
}
int allocated_size() const
{
// return the number of array elements allocated (may != data_size).
return alloc_size;
}
void resize( const Index new_size );
// Expand the valid, indexable number of items in the array.
// Items beyond the current limit are not initialized. An
// array can be shrunk with this function, but only the last
// valid index is changed. Storage is not reclaimed.
Array<Contents> &operator += (const Contents &Data );
Array<Contents> &operator += (const Array<Contents> &Data );
// Note: items added to the array are copies of the original data.
private:
void expand_storage( const Index new_size );
// Expands the internal storage in the array to allow holding
// at least "new_size" items. This does not increase the maximum
// valid index.
Contents *data;
Index data_size, alloc_size;
};
template <class Contents>
Array<Contents>::Array( const Index initial_size, const Boolean initialize )
{
data_size = alloc_size = 0;
data = NULL;
// If "initialize" = True, we want an array filled with data; otherwise,
// we want to allocate storage but consider the array empty.
if (initialize)
resize( initial_size );
else
expand_storage(initial_size);
}
template <class Contents>
Array<Contents>::~Array()
{
delete [] data;
}
template <class Contents>
Array<Contents>::Array( const Array<Contents> &arr)
{
data_size = arr.data_size;
alloc_size = arr.alloc_size;
data = new Contents [arr.alloc_size];
assert(data);
for (Index i = 0; i < data_size; i++)
data[i] = arr.data[i];
}
template <class Contents>
Array <Contents>& Array<Contents>::operator = (const Array<Contents> &arr)
{
if (this != &arr)
{
data_size = arr.data_size;
alloc_size = arr.alloc_size;
data = new Contents [arr.alloc_size];
for (Index i = 0; i < data_size; i++)
data[i] = arr.data[i];
}
return *this;
}
template <class Contents>
Contents& Array<Contents>::operator [](const Index index) const
{
#ifdef RANGE_CHECK
assert (index < data_size);
#endif
return data[index];
}
template <class Contents>
Array<Contents> &Array<Contents>::operator += (const Contents &Data )
{
Index old_data_size = data_size;
resize(data_size + 1);
data[old_data_size] = Data;
return *this;
}
template <class Contents>
Array<Contents> &Array<Contents>::operator += (const Array <Contents>&Data )
{
Index old_size = data_size;
resize(data_size + Data.size());
for (Index i = old_size; i < data_size; i++)
data[i] = Data.data[i-old_size];
return *this;
}
template <class Contents >
void Array<Contents>::expand_storage( const Index new_size )
{
Index size1 = alloc_size + alloc_size/10 + 10;
Index real_new_size = (new_size > size1) ? new_size : size1;
Contents * new_data = new Contents[real_new_size];
assert(new_data);
for (Index i = 0; i<data_size; i++)
new_data[i] = data[i];
delete [] data;
data = new_data;
alloc_size = real_new_size;
}
template <class Contents>
void Array<Contents>::resize( const Index new_size )
{
if (new_size > alloc_size)
expand_storage( new_size );
data_size = new_size;
}
#endif